﻿<!DOCTYPE html>
<html>
<head>
  <title>HTML Drag and Drop</title>
  <!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
  <meta name="description" content="Drag-and-drop HTML elements into a GoJS Diagram using jQuery." />
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="../assets/css/jquery-ui.min.css" />
  <script src="../assets/js/jquery.min.js"></script>
  <script src="../assets/js/jquery-ui.min.js"></script>

  <script src="../release/go.js"></script>
  <script src="../assets/js/goSamples.js"></script>  <!-- this is only for the GoJS Samples framework -->

  <script id="code">
    function init() {
      if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this
      // Note that we do not use $ here as an alias for go.GraphObject.make because we are using $ for jQuery
      var $$ = go.GraphObject.make;  // for conciseness in defining templates

      myDiagram =
        $$(go.Diagram, "myDiagramDiv",  // must name or refer to the DIV HTML element
          { initialPosition: new go.Point(0, 0), "animationManager.isEnabled": false });

      myDiagram.nodeTemplate =
        $$(go.Node, "Auto",
          { locationSpot: go.Spot.Center },
          new go.Binding("location", "loc", go.Point.parse),
          $$(go.Shape, "Ellipse",
            { fill: "white" }),
          $$(go.TextBlock,
            { margin: 5 },
            new go.Binding("text", "text").makeTwoWay()));

      $("li").draggable({
        stack: "#myDiagramDiv",
        revert: true,
        revertDuration: 0
      });

      $("#myDiagramDiv").droppable({
        activeClass: "ui-state-highlight",
        drop: function(event, ui) {
          var elt = ui.draggable.first();
          var text = elt[0].textContent;
          var x = ui.offset.left - $(this).offset().left;
          var y = ui.offset.top - $(this).offset().top;
          var p = new go.Point(x, y);
          var q = myDiagram.transformViewToDoc(p);
          var model = myDiagram.model;
          model.startTransaction("drop");
          model.addNodeData({
            text: text,
            loc: go.Point.stringify(q)
          });
          model.commitTransaction("drop");
        }
      });
    }
  </script>
</head>
<body onload="init()">
<div id="sample">
  <div style="width: 100%; display: flex; justify-content: space-between">
    <div id="myItems" style="width: 120px; height: 700px; margin-right: 2px; border: solid 1px black">
      <ul>
        <li>First</li>
        <li>Second</li>
        <li>Third</li>
        <li>Fourth</li>
        <li>Fifth</li>
      </ul>
    </div>
    <div id="myDiagramDiv" style="flex-grow: 1; height: 700px; border: solid 1px black"></div>
  </div>
  <div id ="description">
  <p>
  This demonstrates using jQuery drag-and-drop capability to allow the user to drag HTML list items into a GoJS diagram.
  </p>
  </div>
</div>
</body>
</html>
